<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
<!--      <el-form-item label="appid" prop="appId">-->
<!--        <el-input v-model="formData.appId" placeholder="请输入appid" />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="应用名称" prop="appName">-->
<!--        <el-input v-model="formData.appName" placeholder="请输入应用名称" />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="应用类型" prop="appType">-->
<!--        <el-select v-model="formData.appType" placeholder="请选择应用类型">-->
<!--          <el-option label="请选择字典生成" value="" />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item label="公众号" prop="appId">
        <el-select v-model="formData.appId">
          <el-option
            v-for="item in accountList"
            :key="item.appId"
            :label="item.name"
            :value="item.appId"
          />
        </el-select>
      </el-form-item>
          <el-form-item label="推送类型" prop="sendType">
            <el-select v-model="formData.sendType" placeholder="请选择推送类型">
              <el-option label="退款申请消息推送" value="TFSH" />
              <el-option label="病案复印消息推送" value="BAFY" />
            </el-select>
          </el-form-item>
<!--      <el-form-item label="推送名称" prop="sendName">-->
<!--        <el-input v-model="formData.sendName" placeholder="请输入推送名称" disabled/>-->
<!--      </el-form-item>-->
      <el-form-item label="姓名" prop="username">
        <el-input v-model="formData.username" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input v-model="formData.phone" placeholder="请输入电话" />
      </el-form-item>
      <el-form-item label="openid" prop="openid">
        <template #label>
          <Tooltip
            message="可前往病人档案功能查找对应openid"
            title="openid"
          />
        </template>
        <el-input v-model="formData.openid" placeholder="请输入openid" />
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio
            v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)"
            :key="dict.label"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { MsgSendUserApi, MsgSendUserVO } from '@/api/hospital/msgsenduser'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import * as MpAccountApi from "@/api/mp/account";
import {getAccountPage} from "@/api/mp/account";

/** 消息推送用户 表单 */
defineOptions({ name: 'MsgSendUserForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const accountList = ref<MpAccountApi.AccountVO[]>([]) // 公众号账号列表
const accountMp = ref<MpAccountApi.AccountVO>() // 公众号账号列表
/** 加载公众号账号的列表 */
const getAccountList = async () => {
  const data = await MpAccountApi.getAccountPage(formData)
  if(data.list.length > 0){
    accountMp.value = data.list[0];
    accountList.value = data.list;
    formData.value.appId = data.list[0].appId;
  }
  // console.table(accountMp.value)
}

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  appId: undefined,
  appName: undefined,
  appType: undefined,
  username: undefined,
  openid: undefined,
  phone: undefined,
  sendType: undefined,
  sendName: undefined,
  status: undefined
})
const formRules = reactive({
  appId: [{ required: true, message: 'appid不能为空', trigger: 'blur' }],
  appName: [{ required: true, message: '应用名称不能为空', trigger: 'blur' }],
  appType: [{ required: true, message: '应用类型不能为空', trigger: 'change' }],
  openid: [{ required: true, message: 'openid不能为空', trigger: 'blur' }],
  status: [{ required: true, message: '状态不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  // 获取公众号列表
  resetForm()
  await getAccountList()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await MsgSendUserApi.getMsgSendUser(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as MsgSendUserVO
    if(data.sendType === 'TFSH'){
      data.sendName ='退款申请消息推送'
    }else if (data.sendType === 'BAFY'){
      data.sendName ='病案复印消息推送'
    }
    data.appId =  accountMp.value.appId
    data.appName =  accountMp.value.name
    data.appType =  '31'
    if (formType.value === 'create') {
      await MsgSendUserApi.createMsgSendUser(data)
      message.success(t('common.createSuccess'))
    } else {
      await MsgSendUserApi.updateMsgSendUser(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    appId: undefined,
    appName: undefined,
    appType: undefined,
    username: undefined,
    openid: undefined,
    phone: undefined,
    sendType: undefined,
    sendName: undefined,
    status: undefined
  }
  formRef.value?.resetFields()
}
</script>
